home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / Library / Implementation / HTParse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-06  |  12.0 KB  |  447 lines

  1. /*        Parse HyperText Document Address        HTParse.c
  2. **        ================================
  3. */
  4.  
  5. #include "HTUtils.h"
  6. #include "HTParse.h"
  7. #include "tcp.h"
  8.  
  9. #define HEX_ESCAPE '%'
  10.  
  11. struct struct_parts {
  12.     char * access;
  13.     char * host;
  14.     char * absolute;
  15.     char * relative;
  16. /*    char * search;        no - treated as part of path */
  17.     char * anchor;
  18. };
  19.  
  20.  
  21. /*    Strip white space off a string
  22. **    ------------------------------
  23. **
  24. ** On exit,
  25. **    Return value points to first non-white character, or to 0 if none.
  26. **    All trailing white space is OVERWRITTEN with zero.
  27. */
  28.  
  29. #ifdef __STDC__
  30. char * HTStrip(char * s)
  31. #else
  32. char * HTStrip(s)
  33.     char *s;
  34. #endif
  35. {
  36. #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
  37.     char * p=s;
  38.     for(p=s;*p;p++);                /* Find end of string */
  39.     for(p--;p>=s;p--) {
  40.         if(SPACE(*p)) *p=0;    /* Zap trailing blanks */
  41.     else break;
  42.     }
  43.     while(SPACE(*s))s++;    /* Strip leading blanks */
  44.     return s;
  45. }
  46.  
  47.  
  48. /*    Scan a filename for its consituents
  49. **    -----------------------------------
  50. **
  51. ** On entry,
  52. **    name    points to a document name which may be incomplete.
  53. ** On exit,
  54. **      absolute or relative may be nonzero (but not both).
  55. **    host, anchor and access may be nonzero if they were specified.
  56. **    Any which are nonzero point to zero terminated strings.
  57. */
  58. #ifdef __STDC__
  59. PRIVATE void scan(char * name, struct struct_parts *parts)
  60. #else
  61. PRIVATE void scan(name, parts)
  62.     char * name;
  63.     struct struct_parts *parts;
  64. #endif
  65. {
  66.     char * after_access;
  67.     char * p;
  68.     int length = strlen(name);
  69.     
  70.     parts->access = 0;
  71.     parts->host = 0;
  72.     parts->absolute = 0;
  73.     parts->relative = 0;
  74.     parts->anchor = 0;
  75.     
  76.     after_access = name;
  77.     for(p=name; *p; p++) {
  78.     if (*p==':') {
  79.         *p = 0;
  80.         parts->access = name;    /* Access name has been specified */
  81.         after_access = p+1;
  82.     }
  83.     if (*p=='/') break;
  84.     if (*p=='#') break;
  85.     }
  86.     
  87.     for(p=name+length-1; p>=name; p--) {
  88.     if (*p =='#') {
  89.         parts->anchor=p+1;
  90.         *p=0;                /* terminate the rest */
  91.     }
  92.     }
  93.     p = after_access;
  94.     if (*p=='/'){
  95.     if (p[1]=='/') {
  96.         parts->host = p+2;        /* host has been specified     */
  97.         *p=0;            /* Terminate access         */
  98.         p=strchr(parts->host,'/');    /* look for end of host name if any */
  99.         if(p) {
  100.             *p=0;            /* Terminate host */
  101.             parts->absolute = p+1;        /* Root has been found */
  102.         }
  103.     } else {
  104.         parts->absolute = p+1;        /* Root found but no host */
  105.     }        
  106.     } else {
  107.         parts->relative = (*after_access) ? after_access : 0;    /* zero for "" */
  108.     }
  109.  
  110.     /* Access specified but no host: the anchor was not really one
  111.        e.g. news:j462#36487@foo.bar -- JFG 10/7/92, from bug report */
  112.     if (parts->access && ! parts->host && parts->anchor) {
  113.       *(parts->anchor - 1) = '#';  /* Restore the '#' in the address */
  114.       parts->anchor = 0;
  115.     }
  116.  
  117. #ifdef NOT_DEFINED    /* search is just treated as part of path */
  118.     {
  119.         char *p = relative ? relative : absolute;
  120.     if (p) {
  121.         char * q = strchr(p, '?');    /* Any search string? */
  122.         if (q) {
  123.             *q = 0;            /* If so, chop that off. */
  124.         parts->search = q+1;
  125.         }
  126.     }
  127.     }
  128. #endif
  129. } /*scan */    
  130.  
  131.  
  132. /*    Parse a Name relative to another name
  133. **    -------------------------------------
  134. **
  135. **    This returns those parts of a name which are given (and requested)
  136. **    substituting bits from the related name where necessary.
  137. **
  138. ** On entry,
  139. **    aName        A filename given
  140. **      relatedName     A name relative to which aName is to be parsed
  141. **      wanted          A mask for the bits which are wanted.
  142. **
  143. ** On exit,
  144. **    returns        A pointer to a malloc'd string which MUST BE FREED
  145. */
  146. #ifdef __STDC__
  147. char * HTParse(const char * aName, const char * relatedName, int wanted)
  148. #else
  149. char * HTParse(aName, relatedName, wanted)
  150.     char * aName;
  151.     char * relatedName;
  152.     int wanted;
  153. #endif
  154.  
  155. {
  156.     char * result = 0;
  157.     char * return_value = 0;
  158.     int len;
  159.     char * name = 0;
  160.     char * rel = 0;
  161.     char * p;
  162.     struct struct_parts given, related;
  163.     
  164.     /* Make working copies of input strings to cut up:
  165.     */
  166.     len = strlen(aName)+strlen(relatedName)+10;
  167.     result=(char *)malloc(len);        /* Lots of space: more than enough */
  168.     if (result == NULL) outofmem(__FILE__, "HTParse");
  169.     
  170.     StrAllocCopy(name, aName);
  171.     StrAllocCopy(rel, relatedName);
  172.     
  173.     scan(name, &given);
  174.     scan(rel,  &related); 
  175.     result[0]=0;        /* Clear string  */
  176.     if (wanted & PARSE_ACCESS)
  177.         if (given.access|| related.access) {
  178.         strcat(result, given.access ? given.access : related.access);
  179.         if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
  180.     }
  181.     
  182.     if (given.access && related.access)    /* If different, inherit nothing. */
  183.         if (strcmp(given.access, related.access)!=0) {
  184.         related.host=0;
  185.         related.absolute=0;
  186.         related.relative=0;
  187.         related.anchor=0;
  188.     }
  189.     
  190.     if (wanted & PARSE_HOST)
  191.         if(given.host || related.host) {
  192.         if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
  193.         strcat(result, given.host ? given.host : related.host);
  194.     }
  195.     
  196.     if (given.host && related.host)  /* If different hosts, inherit no path. */
  197.         if (strcmp(given.host, related.host)!=0) {
  198.         related.absolute=0;
  199.         related.relative=0;
  200.         related.anchor=0;
  201.     }
  202.     
  203.     if (wanted & PARSE_PATH) {
  204.         if(given.absolute) {                /* All is given */
  205.         if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
  206.         strcat(result, given.absolute);
  207.     } else if(related.absolute) {    /* Adopt path not name */
  208.         strcat(result, "/");
  209.         strcat(result, related.absolute);
  210.         if (given.relative) {
  211.         p = strchr(result, '?');    /* Search part? */
  212.         if (!p) p=result+strlen(result)-1;
  213.         for (; *p!='/'; p--);    /* last / */
  214.         p[1]=0;                    /* Remove filename */
  215.         strcat(result, given.relative);        /* Add given one */
  216.         HTSimplify (result);
  217.         }
  218.     } else if(given.relative) {
  219.         strcat(result, given.relative);        /* what we've got */
  220.     } else if(related.relative) {
  221.         strcat(result, related.relative);
  222.     } else {  /* No inheritance */
  223.         strcat(result, "/");
  224.     }
  225.     }
  226.         
  227.     if (wanted & PARSE_ANCHOR)
  228.         if(given.anchor || related.anchor) {
  229.         if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
  230.         strcat(result, given.anchor ? given.anchor : related.anchor);
  231.     }
  232.     free(rel);
  233.     free(name);
  234.     
  235.     StrAllocCopy(return_value, result);
  236.     free(result);
  237.     return return_value;        /* exactly the right length */
  238. }
  239.  
  240.  
  241. /*            Simplify a filename
  242. //        -------------------
  243. //
  244. // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
  245. // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
  246. // Simplification helps us recognize duplicate filenames.
  247. //
  248. //    Thus,     /etc/junk/../fred     becomes    /etc/fred
  249. //        /etc/junk/./fred    becomes    /etc/junk/fred
  250. //
  251. //      but we should NOT change
  252. //        http://fred.xxx.edu/../..
  253. //
  254. //    or    ../../albert.html
  255. */
  256. #ifdef __STDC__
  257. void HTSimplify(char * filename)
  258. #else
  259. void HTSimplify(filename)
  260.     char * filename;
  261. #endif
  262.  
  263. {
  264.     char * p;
  265.     char * q;
  266.     if (filename[0] && filename[1])    /* Bug fix 12 Mar 93 TBL */
  267.      for(p=filename+2; *p; p++) {
  268.         if (*p=='/') {
  269.         if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
  270.         for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
  271.         if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
  272.             &&!(q-1>filename && q[-1]=='/')) {
  273.                 strcpy(q, p+3);    /* Remove  /xxx/..    */
  274.             if (!*filename) strcpy(filename, "/");
  275.             p = q-1;        /* Start again with prev slash     */
  276.         } else {            /*   xxx/.. leave it!    */
  277. #ifdef BUG_CODE
  278.             strcpy(filename, p[3] ? p+4 : p+3); /* rm  xxx/../    */
  279.             p = filename;        /* Start again */
  280. #endif
  281.         }
  282.         } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
  283.             strcpy(p, p+2);            /* Remove a slash and a dot */
  284.         }
  285.     }
  286.     }
  287. }
  288.  
  289.  
  290. /*        Make Relative Name
  291. **        ------------------
  292. **
  293. ** This function creates and returns a string which gives an expression of
  294. ** one address as related to another. Where there is no relation, an absolute
  295. ** address is retured.
  296. **
  297. **  On entry,
  298. **    Both names must be absolute, fully qualified names of nodes
  299. **    (no anchor bits)
  300. **
  301. **  On exit,
  302. **    The return result points to a newly allocated name which, if
  303. **    parsed by HTParse relative to relatedName, will yield aName.
  304. **    The caller is responsible for freeing the resulting name later.
  305. **
  306. */
  307. #ifdef __STDC__
  308. char * HTRelative(const char * aName, const char *relatedName)
  309. #else
  310. char * HTRelative(aName, relatedName)
  311.    char * aName;
  312.    char * relatedName;
  313. #endif
  314. {
  315.     char * result = 0;
  316.     CONST char *p = aName;
  317.     CONST char *q = relatedName;
  318.     CONST char * after_access = 0;
  319.     CONST char * path = 0;
  320.     CONST char * last_slash = 0;
  321.     int slashes = 0;
  322.     
  323.     for(;*p; p++, q++) {    /* Find extent of match */
  324.         if (*p!=*q) break;
  325.     if (*p==':') after_access = p+1;
  326.     if (*p=='/') {
  327.         last_slash = p;
  328.         slashes++;
  329.         if (slashes==3) path=p;
  330.     }
  331.     }
  332.     
  333.     /* q, p point to the first non-matching character or zero */
  334.     
  335.     if (!after_access) {            /* Different access */
  336.         StrAllocCopy(result, aName);
  337.     } else if (slashes<3){            /* Different nodes */
  338.         StrAllocCopy(result, after_access);
  339.     } else if (slashes==3){            /* Same node, different path */
  340.         StrAllocCopy(result, path);
  341.     } else {                    /* Some path in common */
  342.         int levels= 0;
  343.         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
  344.     result = (char *)malloc(3*levels + strlen(last_slash) + 1);
  345.       if (result == NULL) outofmem(__FILE__, "HTRelative");
  346.     result[0]=0;
  347.     for(;levels; levels--)strcat(result, "../");
  348.     strcat(result, last_slash+1);
  349.     }
  350.     if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n    `%s' is\n   `%s'.",
  351.             aName, relatedName, result);
  352.     return result;
  353. }
  354.  
  355.  
  356. /*        Escape undesirable characters using %        HTEscape()
  357. **        -------------------------------------
  358. **
  359. **    This function takes a pointer to a string in which
  360. **    some characters may be unacceptable unescaped.
  361. **    It returns a string which has these characters
  362. **    represented by a '%' character followed by two hex digits.
  363. **
  364. **    Unlike HTUnEscape(), this routine returns a malloced string.
  365. */
  366.  
  367. PRIVATE CONST unsigned char isAcceptable[96] =
  368.  
  369. /*    Bit 0        xalpha        -- see HTFile.h
  370. **    Bit 1        xpalpha        -- as xalpha but with plus.
  371. **    Bit 3 ...    path        -- as xpalphas but with /
  372. */
  373.     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
  374.     {    0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,    /* 2x   !"#$%&'()*+,-./     */
  375.          7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,    /* 3x  0123456789:;<=>?     */
  376.      7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,    /* 4x  @ABCDEFGHIJKLMNO  */
  377.      7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,    /* 5X  PQRSTUVWXYZ[\]^_     */
  378.      0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,    /* 6x  `abcdefghijklmno     */
  379.      7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };    /* 7X  pqrstuvwxyz{\}~    DEL */
  380.  
  381. PRIVATE char *hex = "0123456789ABCDEF";
  382.  
  383. PUBLIC char * HTEscape ARGS2 (CONST char *, str,
  384.     unsigned char, mask)
  385. {
  386. #define ACCEPTABLE(a)    ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
  387.     CONST char * p;
  388.     char * q;
  389.     char * result;
  390.     int unacceptable = 0;
  391.     for(p=str; *p; p++)
  392.         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
  393.         unacceptable++;
  394.     result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
  395.     if (result == NULL) outofmem(__FILE__, "HTEscape");
  396.     for(q=result, p=str; *p; p++) {
  397.         unsigned char a = TOASCII(*p);
  398.     if (!ACCEPTABLE(a)) {
  399.         *q++ = HEX_ESCAPE;    /* Means hex commming */
  400.         *q++ = hex[a >> 4];
  401.         *q++ = hex[a & 15];
  402.     }
  403.     else *q++ = *p;
  404.     }
  405.     *q++ = 0;            /* Terminate */
  406.     return result;
  407. }
  408.  
  409.  
  410. /*        Decode %xx escaped characters            HTUnEscape()
  411. **        -----------------------------
  412. **
  413. **    This function takes a pointer to a string in which some
  414. **    characters may have been encoded in %xy form, where xy is
  415. **    the acsii hex code for character 16x+y.
  416. **    The string is converted in place, as it will never grow.
  417. */
  418.  
  419. PRIVATE char from_hex ARGS1(char, c)
  420. {
  421.     return  c >= '0' && c <= '9' ?  c - '0' 
  422.             : c >= 'A' && c <= 'F'? c - 'A' + 10
  423.             : c - 'a' + 10;    /* accept small letters just in case */
  424. }
  425.  
  426. PUBLIC char * HTUnEscape ARGS1( char *, str)
  427. {
  428.     char * p = str;
  429.     char * q = str;
  430.     while(*p) {
  431.         if (*p == HEX_ESCAPE) {
  432.         p++;
  433.         if (*p) *q = from_hex(*p++) * 16;
  434.         if (*p) *q = FROMASCII(*q + from_hex(*p++));
  435.         q++;
  436.     } else {
  437.         *q++ = *p++; 
  438.     }
  439.     }
  440.     
  441.     *q++ = 0;
  442.     return str;
  443.     
  444. } /* HTUnEscape */
  445.  
  446.  
  447.